$(document).ready(function(){
	$(".left_li").hover(function(){
		//菜单关闭的时候
		if($(".page_content").hasClass("page_left_off"))
		{
			$(this).addClass("left_chover");
			$(this).find(".left_linav").show();
		}
		//菜单打开的时候
		else
		{
			//当前位置
			if($(this).hasClass("left_lion"))
			{
			}
			//其他位置
			else
			{
				$(this).find(".left_linav").show();
			}
		}
	},function(){
		//菜单关闭的时候
		if($(".page_content").hasClass("page_left_off"))
		{
			$(this).removeClass("left_chover");
			$(this).find(".left_linav").hide();
		}
		//菜单打开的时候
		else
		{
			//当前位置
			if($(this).hasClass("left_lion"))
			{
			}
			//其他位置
			else
			{
				$(this).find(".left_linav").hide();
			}
		}
	});
});

//判断浏览器类型
var qgExploer = navigator.appName;
var qgIE;
if(qgExploer == "Microsoft Internet Explorer")
{
	qgIE = "IE";
	if(navigator.appVersion.match(/7./i)!='7.')
	{
		qgIE = "IE6";
	}
}
else
{
	qgIE = "FF";
}

function qgopen(url,height,inputname)
{
	qg_open(url,height,inputname);
}

//改变样式
function skin_change(o,skintype)
{
	//如果样式与原来的相同则不改变
	skinnow = document.getElementById("skinnow").value;
	if(skinnow==skintype)
	{
		return false;
	}
	//$(".skin_name").remove();
	$.ajax({
		url:mainlink+"?file=common&act=skinChange&skin="+skintype,
		dataType:"json",
		success: function(data)
		{
			if(data.state==1)
			{
				location.reload();
				/*var link = document.createElement("link");
				link.rel = "stylesheet";
				link.type = "text/css";
				link.id = "skin_"+skintype; 
				link.className = "skin_name";
				link.href = "/chengxu/sycms/admin/tpl/css/skin/"+skintype+"/skin.css";
				document.getElementsByTagName("head")[0].appendChild(link);
				document.getElementById("skinnow").value = skintype;*/
			}
		}
	});
	//$(o).addClass("skin_type_on").siblings().removeClass("skin_type_on");
}

//收起菜单
function left_close(act)
{
	left_act = $(".left_"+act);
	if($(".page_content").hasClass("page_left_off"))
	{
		left_click("open");
		left_act.find(".left_linav").show();
	}
	else
	{
		left_click("close");
		left_act.find(".left_linav").hide();
	}
	$(".page_content").toggleClass("page_left_off");
}

//标签切换
function tabs(thisli,divname)
{
	$("#"+divname).show().siblings(".tabdiv").hide();
	$(thisli).addClass("tabon").siblings("li").removeClass("tabon");
}

function qgurl(url)
{
	window.location.href=url;
}

//显示图片
function tip_img(url)
{
	html = '<div class="tip_img"><table><tr><td><a href="'+url+'" target="_blank"><div class="pad"><img src="'+url+'" /></div></a></td></tr></table></div>';
	layer.open({
		type: 1,
		title: false,
		area: ['800px', '80%'],
		shadeClose: true, //开启遮罩关闭
		content: html
	});
}

//警告提示-链接跳转
function tip_confirm(tip,url)
{
	parent.document.onkeydown = function(e)
	{
		var ev =parent. document.all ? window.event : e;  
		if(ev.keyCode==13)
		{
			return false
		}
	}
	document.onkeydown = function(e)
	{
		var ev =document.all ? window.event : e;
		if(ev.keyCode==13)
		{
			return false
		}
	}
	
	layer.confirm(tip, {
		icon: 7,
		title: "警告",
		skin: "layui-layer-my1",
		shadeClose: true //开启遮罩关闭
	}, function(){
		//确定
		qgurl(url);
	}, function(){
		//取消
	});
}

//浮动提示
function tip_msg(tip)
{
	layer.msg(tip,{time:1000});
}

//提示
function tip_alert(tip)
{
	layer.confirm(tip, {
		icon: 2,
		title: "提示",
		skin: "layui-layer-my1",
		time : 4000,
		shadeClose: true, //开启遮罩关闭
		success: function(layero,index){
			//var msg = layero.text();
			var i = 3;
			var timer = null;
			var fn = function() {
				icon_html = '<i class="layui-layer-ico layui-layer-ico2"></i>';
				layero.find(".layui-layer-content").html(icon_html+tip+'('+i+')');
				if(!i) {
					layer.close(index);
					clearInterval(timer);
				} 
				i--;
			};
			timer = setInterval(fn, 1000);
			fn();
			},
		}, function() {
			
	});
}

//批量上传
function tip_uppic(tit,url)
{
	layer.open({
		type: 2,
		title: tit,
		shadeClose: true,
		shade: 0.6,
		shadeClose: true, //开启遮罩关闭
		maxmin: true, //开启最大化最小化按钮
		area: ['900px', '82%'],
		content: url,
		end: function(){
			location.reload();
		} 
	});
}

//随机密码
function randpass(dstObj,charsLength)
{
	var dstElem = document.getElementById(dstObj);
	var length = charsLength;
	var charsA = "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	//var charsB = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var charsC = "0123456789";
	var charsD = "~!@#%^&*()\-_=+{}<>?";
	var randomChars = "";
	for(x=0; x<3; x++) {
		var i = Math.floor(Math.random() * charsA.length);
		randomChars += charsA.charAt(i);
	}
	/*
	for(x=0; x<2; x++) { 
		var i = Math.floor(Math.random() * charsB.length); 
		randomChars += charsB.charAt(i); 
	}*/
	for(x=0; x<3; x++) {
		var i = Math.floor(Math.random() * charsC.length);
		randomChars += charsC.charAt(i);
	}
	for(x=0; x<2; x++) {
		var i = Math.floor(Math.random() * charsD.length);
		randomChars += charsD.charAt(i);
	}
	dstElem.value = randomChars;
}

// 修改指定表的指定字段值
function changeTableVal(table,id_name,id_value,field,obj)
{
	var src = "";
	if($(obj).attr('src').indexOf("cancel.png")>0)
	{
		src = 'admin/tpl/images/yes.png';
		var value = 1;
	}
	else
	{
		src = 'admin/tpl/images/cancel.png';
		var value = 0;
	}
	$.ajax({
	type:"post",
	url:mainlink+"?file=common&act=changeTableVal",
	data:{"table":table,"id_name":id_name,"id_value":id_value,"field":field,"gvalue":value},
	dataType:"json",
	
	success:function(data)
	{
		if(data.state==1)
		{
			$(obj).attr('src',src);
		}
	}
	});
}

//复制文本
function copytext(from,to)
{
	val = document.getElementById(from).value;
	document.getElementById(to).value = val;
}

function UrlEncode(str)
{
	return transform(str);
}

function transform(s)
{
	var hex=''
	var i,j,t

	j=0
	for (i=0; i<s.length; i++)
	{
		t = hexfromdec( s.charCodeAt(i) );
		if (t=='25')
		{
			t='';
		}
		hex += '%' + t;
	}
	return hex;
}

function hexfromdec(num) {
        if (num > 65535) { return ("err!") }
        first = Math.round(num/4096 - .5);
        temp1 = num - first * 4096;
        second = Math.round(temp1/256 -.5);
        temp2 = temp1 - second * 256;
        third = Math.round(temp2/16 - .5);
        fourth = temp2 - third * 16;
        return (""+getletter(third)+getletter(fourth));
}

function getletter(num) {
        if (num < 10) {
                return num;
        }
        else {
            if (num == 10) { return "A" }
            if (num == 11) { return "B" }
            if (num == 12) { return "C" }
            if (num == 13) { return "D" }
            if (num == 14) { return "E" }
            if (num == 15) { return "F" }
        }
}

function qg_open(url,height)
{
	//判断高度小于或等于100时，变为154，主要是兼容Firefox浏览器
	if(height < 154)
	{
		height = 154;
	}
	if(qgIE == "IE" || qgIE == "IE6")
	{
		var iframe_height;
		if(qgIE == "IE6")
		{
			iframe_height = eval(height + " - 28");
			height = eval(height + " + 53");
		}
		else
		{
			iframe_height = eval(height + " - 28");
		}
		showModalDialog(mainlink+"?incfile="+UrlEncode(url)+"&iframe_height="+iframe_height,window,"dialogWidth:700px;dialogHeight:"+ height +"px;help:no;scroll:no;status:yes;resizable:no");
	}
	else
	{
		qinggan_left = (screen.width-700)/2;
		qinggan_win_option = "modal=yes,location=no,menubar=no,toolbar=no,dependent=yes,dialog=yes,status=0,minimizable=no,alwaysRaised,resizable=yes,scrollbars=no,width=700,height="+height+",top=180,left="+qinggan_left;
		window.open(mainlink+"?incfile="+UrlEncode(url)+"&iframe_height="+(height-30),"_blank",qinggan_win_option);
	}
}

function qg_close()
{
	window.close();
}

function get_url()
{
	var str = location.pathname;
	a = str.substr(str.lastIndexOf("/")+1);
	var url = a+window.location.search;
	return url;
}

function setTab(m,n)
{
	var tli=document.getElementById("menu"+m).getElementsByTagName("li");
	var mli=document.getElementById("main"+m).getElementsByTagName("dl");
	for(i=0;i<tli.length;i++){
	tli[i].className=i==n?"hover":"";
	mli[i].className=i==n?"block":"";
	}
}

function Base64() {  
   
    // private property  
    _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";  
   
    // public method for encoding  
    this.encode = function (input) {  
        var output = "";  
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;  
        var i = 0;  
        input = _utf8_encode(input);  
        while (i < input.length) {  
            chr1 = input.charCodeAt(i++);  
            chr2 = input.charCodeAt(i++);  
            chr3 = input.charCodeAt(i++);  
            enc1 = chr1 >> 2;  
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);  
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);  
            enc4 = chr3 & 63;  
            if (isNaN(chr2)) {  
                enc3 = enc4 = 64;  
            } else if (isNaN(chr3)) {  
                enc4 = 64;  
            }  
            output = output +  
            _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +  
            _keyStr.charAt(enc3) + _keyStr.charAt(enc4);  
        }  
        return output;  
    }  
   
    // public method for decoding  
    this.decode = function (input) {  
        var output = "";  
        var chr1, chr2, chr3;  
        var enc1, enc2, enc3, enc4;  
        var i = 0;  
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");  
        while (i < input.length) {  
            enc1 = _keyStr.indexOf(input.charAt(i++));  
            enc2 = _keyStr.indexOf(input.charAt(i++));  
            enc3 = _keyStr.indexOf(input.charAt(i++));  
            enc4 = _keyStr.indexOf(input.charAt(i++));  
            chr1 = (enc1 << 2) | (enc2 >> 4);  
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);  
            chr3 = ((enc3 & 3) << 6) | enc4;  
            output = output + String.fromCharCode(chr1);  
            if (enc3 != 64) {  
                output = output + String.fromCharCode(chr2);  
            }  
            if (enc4 != 64) {  
                output = output + String.fromCharCode(chr3);  
            }  
        }  
        output = _utf8_decode(output);  
        return output;  
    }  
   
    // private method for UTF-8 encoding  
    _utf8_encode = function (string) {  
        string = string.replace(/\r\n/g,"\n");  
        var utftext = "";  
        for (var n = 0; n < string.length; n++) {  
            var c = string.charCodeAt(n);  
            if (c < 128) {  
                utftext += String.fromCharCode(c);  
            } else if((c > 127) && (c < 2048)) {  
                utftext += String.fromCharCode((c >> 6) | 192);  
                utftext += String.fromCharCode((c & 63) | 128);  
            } else {  
                utftext += String.fromCharCode((c >> 12) | 224);  
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);  
                utftext += String.fromCharCode((c & 63) | 128);  
            }  
   
        }  
        return utftext;  
    }  
   
    // private method for UTF-8 decoding  
    _utf8_decode = function (utftext) {  
        var string = "";  
        var i = 0;  
        var c = c1 = c2 = 0;  
        while ( i < utftext.length ) {  
            c = utftext.charCodeAt(i);  
            if (c < 128) {  
                string += String.fromCharCode(c);  
                i++;  
            } else if((c > 191) && (c < 224)) {  
                c2 = utftext.charCodeAt(i+1);  
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));  
                i += 2;  
            } else {  
                c2 = utftext.charCodeAt(i+1);  
                c3 = utftext.charCodeAt(i+2);  
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));  
                i += 3;  
            }  
        }  
        return string;  
    }  
}

//获取下拉框选中值
function get_selected(divid)
{
	var obj = document.getElementById(divid); //定位id
	var index = obj.selectedIndex; // 选中索引
	var result = obj.options[index].value; // 选中值
	return result;
}

//只能输入数字，宽高只取一个
function thisnum(o)
{
	num = Number(o.value);
	if(isNaN(num))
	{
		o.value = "";
	}
	id = $(o).attr("id");
	if(id=="width" && num)
	{
		document.getElementById("height").value = "";
	}
	else if(id=="height" && num)
	{
		document.getElementById("width").value = "";
	}
}